package com.supor.aiot.common.utils;

import android.app.Activity;
import android.content.Intent;

import com.supor.aiot.common.app.Constants;
import com.supor.aiot.common.entity.DeviceInfo;
import com.supor.aiot.common.entity.ProductType;
import com.supor.aiot.common.helper.GlobalDataCache;
import com.supor.aiot.common.ui.activity.NFCCheckActivity;

/**
 * @version: V1.0
 * @author: Xiaozhuang
 * @packageName: com.supor.aiot.common.utils.NFCCheckUtils
 * @description:
 * @date: 2022/1/4
 **/
public class NFCCheckUtils {

    public static final String STATUS = "STATUS";

    public static final int STATUS_NO_EXIT = 0;

    public static final int STATUS_ILLEGAL = 1;

    public static final int STATUS_ILLEGAL_MEDIA = 2;

    public static final int STATUS_ILLEGAL_HAIER = 3;

    public static final int STATUS_ILLEGAL_JOYA = 4;

    public static final int STATUS_ILLEGAL_XIAOMI = 5;

    public static void startResultActivity(Activity activity, int status, boolean isFinish) {
        Intent intent = new Intent(activity, NFCCheckActivity.class);
        intent.putExtra(STATUS, status);
        activity.startActivity(intent);
        if (isFinish) activity.finish();
    }

    public static DeviceInfo getVirtualDeviceByPid(String pid) {
        if (GlobalDataCache.deviceTree != null && GlobalDataCache.deviceTree.device != null) {
            for (DeviceInfo info : GlobalDataCache.deviceTree.device) {
                // 只针对NFC模块
                if (Constants.Type.TYPE_BLENDER_Q8.equalsIgnoreCase(pid)
                        || Constants.Type.ELECTRIC_SAUCEPAN_C18.equalsIgnoreCase(pid)) {
                    // 虚拟mac(pid+标志位+时间戳秒)共15位
                    if (pid.equalsIgnoreCase(info.pid) && info.mac.length() == 15) {
                        return info;
                    }
                }
            }
        }
        return null;
    }

    public static boolean checkPidAccess(String pid) {
        if (GlobalDataCache.productTypeList != null) {
            for (ProductType productType : GlobalDataCache.productTypeList) {
                if (productType.Children == null) {
                    continue;
                }
                for (ProductType.ProductModel child : productType.Children) {
                    if (pid.equalsIgnoreCase(child.productId)) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    public static DeviceInfo getDeviceInfoByPid(String pid) {
        if (GlobalDataCache.deviceTree != null && GlobalDataCache.deviceTree.device != null) {
            for (DeviceInfo info : GlobalDataCache.deviceTree.device) {
                if (pid.equalsIgnoreCase(info.pid)) {
                    return info;
                }
            }
        }
        return null;
    }

    public static String generateUid(String pid, boolean isNfc) {
        String uid = pid;
        if (isNfc) {
            uid += "1";
        } else {
            uid += "0";
        }
        uid += (int) (System.currentTimeMillis() / 1000);
        return uid;
    }
}
